package com.example.actionbar;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import android.annotation.SuppressLint;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.MenuItemCompat;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnActionExpandListener;
import android.view.ViewConfiguration;
import android.view.Window;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.Toast;

/**
 * MyActionBar
 *****************************************************
 * <hr>
 * <dt><span class="strong">类功能简介:</span></dt>
 * <dd>MyActionBar</dd>
 * <dt><span class="strong">创建时间:</span></dt>
 * <dd>2014-8-1 上午11:57:03</dd>
 * <dt><span class="strong">公司:</span></dt>
 * <dd>CorpIt</dd>
 * @author aa1000777 - Email:aa1000777@qq.com
 *****************************************************
 */
public class MainActivity extends FragmentActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		setOverflowShowingAlways();
		doSearchQuery(getIntent()); 
	}

	@SuppressLint("NewApi") @Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
	    MenuItem searchItem = menu.findItem(R.id.action_search);  
	    searchItem.setOnActionExpandListener(new OnActionExpandListener() {  
	        @Override  
	        public boolean onMenuItemActionExpand(MenuItem item) {  
	        	Toast.makeText(MainActivity.this, "打开搜索", Toast.LENGTH_SHORT).show();
	            return true;  
	        }  
	          
	        @Override  
	        public boolean onMenuItemActionCollapse(MenuItem item) {  
	        	Toast.makeText(MainActivity.this, "关闭搜索", Toast.LENGTH_SHORT).show();
	            return true;  
	        }  
	    });  
	    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
	    searchView.setOnQueryTextListener(new OnQueryTextListener() {
			
			@Override
			public boolean onQueryTextSubmit(String query) {
				// TODO Auto-generated method stub
				System.out.println("--------onQueryTextSubmit---------------");
				return false;
			}
			
			@Override
			public boolean onQueryTextChange(String newText) {
				/**
				 *  得到输入框中的数据
				 */
				System.out.println("----------onQueryTextChange-------------"+newText);
				return false;
			}
		});
		return true;
	}

	@Override
	public boolean onMenuOpened(int featureId, Menu menu) {
		if (featureId == Window.FEATURE_ACTION_BAR && menu != null) {
			if (menu.getClass().getSimpleName().equals("MenuBuilder")) {
				try {
					Method m = menu.getClass().getDeclaredMethod(
							"setOptionalIconsVisible", Boolean.TYPE);
					m.setAccessible(true);
					m.invoke(menu, true);
				} catch (Exception e) {
				}
			}
		}
		return super.onMenuOpened(featureId, menu);
	}

	private void setOverflowShowingAlways() {
		try {
			ViewConfiguration config = ViewConfiguration.get(this);
			Field menuKeyField = ViewConfiguration.class
					.getDeclaredField("sHasPermanentMenuKey");
			menuKeyField.setAccessible(true);
			menuKeyField.setBoolean(config, false);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			Toast.makeText(this, "Home", Toast.LENGTH_SHORT).show();
			return true;
		case R.id.action_search:
			Toast.makeText(this, "搜索", Toast.LENGTH_SHORT).show();
			return true;
		case R.id.action_plus:
			Toast.makeText(this, "功能", Toast.LENGTH_SHORT).show();
			return true;
		case R.id.action_plus2:
			Toast.makeText(this, "功能2", Toast.LENGTH_SHORT).show();
			return true;
		case R.id.action_album:
			Toast.makeText(this, "我的相册", Toast.LENGTH_SHORT).show();
			return true;
		case R.id.action_collection:
			Toast.makeText(this, "我的收藏", Toast.LENGTH_SHORT).show();
			return true;
		case R.id.action_card:
			Toast.makeText(this, "我的银行卡", Toast.LENGTH_SHORT).show();
			return true;
		case R.id.action_settings:
			Toast.makeText(this, "设置", Toast.LENGTH_SHORT).show();
			return true;
		case R.id.action_feed:
			Toast.makeText(this, "意见反馈", Toast.LENGTH_SHORT).show();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	
	
	
  
  
    @Override
	protected void onNewIntent(Intent intent) {
		// TODO Auto-generated method stub
		super.onNewIntent(intent);
		System.out.println("--------------------------");
		 doSearchQuery(intent);   
	}

	// 对searchable activity的调用仍是标准的intent，我们可以从intent中获取信息，即要搜索的内容  
    private void doSearchQuery(Intent intent){    
        if(intent == null)   
            return;   
  
        String queryAction = intent.getAction(); 
        System.out.println("内容1："+queryAction); 
        if( Intent.ACTION_SEARCH.equals( intent.getAction())){  //如果是通过ACTION_SEARCH来调用，即如果通过搜索调用   
            String queryString = intent.getStringExtra(SearchManager.QUERY); //获取搜索内容  
            System.out.println("内容2："+queryString); 
            }    
           
    }  
}